﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace studentskicentarETFa
{
    partial class DAL
    {
        public class TerminDAO: IDaoCrud<Termin>
        {
            protected MySqlCommand komanda;

            public long create(Termin entitet)
            {
                try
                {
                    komanda = new MySqlCommand("insert into Termini values ('" +
                        entitet.BrojLjudi + "', '" + entitet.Instruktorid.ToString() + "', ToDate('" + entitet.DatumOdrzavanja.ToString() +
                        "', DD.MM.YYYY)", con);
                    komanda.ExecuteNonQuery();
                    return komanda.LastInsertedId;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Termin read(Termin entitet)
            {
                try
                {
                    komanda = new MySqlCommand("select * from Termini where termin_ID = '" + entitet.IdTermin.ToString() + "';", con);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    Termin termin = null;
                    while (citac.Read())
                    {
                        termin = new Termin(citac.GetDateTime("datum"), citac.GetString("instruktor"), citac.GetInt32("broj"), citac.GetString("id"));
                        return termin;
                    }
                    return termin;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Termin update(Termin entitet)
            {
                try
                {
                    komanda = new MySqlCommand("update Termini set termin = '" +
                        entitet.BrojLjudi + "', '" + entitet.Instruktorid.ToString() + "', ToDate('" + entitet.DatumOdrzavanja.ToString() +
                        "', DD.MM.YYYY)", con);
                    komanda.ExecuteNonQuery();
                    return read(entitet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public void delete(Termin entitet)
            {
                try
                {
                    komanda = new MySqlCommand("delete from Uposlenici where id = " + entitet.IdTermin + ";", con);
                    komanda.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public List<Termin> getAll()
            {
                try
                {
                    komanda = new MySqlCommand("select * from termin", con);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    List<Termin> termini = new List<Termin>();
                    while (citac.Read())
                        termini.Add(new Termin(citac.GetDateTime("datum"), citac.GetString("instruktor"), citac.GetInt32("broj"), citac.GetString("id")));
                    return termini;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Termin> getByExample(string name, string value)
            {
                try
                {
                    komanda = new MySqlCommand("select ‘from Termin where '" + value + "'='" + name + "'", con);
                    List<Termin> termini = new List<Termin>();
                    MySqlDataReader citac = komanda.ExecuteReader();
                    while (citac.Read())
                    {
                        termini.Add(new Termin(citac.GetDateTime("datum"), citac.GetString("instruktor"), citac.GetInt32("broj"), citac.GetString("id")));
                    }
                    return termini;
                }


                catch (Exception e)
                {
                    throw e;
                }
            }

                  public Termin getById(int id)
            {
                try
                {
                    komanda = new MySqlCommand("select ‘from Termin where '"+id + "'", con);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    Termin termini = null;
                    while (citac.Read())
                    {
                        termini=new Termin(citac.GetDateTime("datum"), citac.GetString("instruktor"), citac.GetInt32("broj"), citac.GetString("id"));
                    }
                    return termini;
                }
                catch (Exception e)
                {
                    throw e;
                }

            }
             

        }
    }
}
